// 给退出注册点击
$('.logout').on('click', function () {
  
    // 弹出询问框
    layer.confirm('是否要退出登录?', {icon: 3, title:'提示'}, function(index){
      //do something
      
      // 清空token
      localStorage.removeItem('token')
      // 跳转页面
      location.href='/login.html'

      layer.close(index);
    });

})

// 接收用户信息
function initUserInfo() {
  
  axios.get('/my/userinfo', {
    headers:{
      Authorization:localStorage.getItem('token')
  }
  })
    .then(({ data: res }) => {
    console.log('成功了')
      console.log(res)
      renderUserInfo(res.data)
    },(error) => {
      console.log('失败了')
      console.dir(error)

      if (error.response.status === 401) {
        localStorage.removeItem('token')
        location.href='/login.html'
      }
    }
  )
 
}
initUserInfo()

// 更新提交用户信息
function renderUserInfo(data) {
  console.log(data)
// 取首字母
const name=data.nickname||data.username
const textAvatar=name.charAt(0).toUpperCase()
  // 判断用户是否有头像图
  if (data.user_pic) {
    $('#header-avatar').html(` <img src="${data.user_pic}"
    class="layui-nav-img">
  
  个人中心`)
  } else {
    
    $('#header-avatar').html(`  <div class="text-avatar">${textAvatar}</div>
    个人中心`)
  }
  if (data.user_pic) {
    $('.user-info-box').html(`  <img src="${data.user_pic}"
    class="layui-nav-img">
 
  <span class="welcome">&nbsp;欢迎&nbsp;${name}</span>`)
  } else {
    // 取首字母
   
    $('.user-info-box').html(`  <div class="text-avatar">${textAvatar}</div>
    <span class="welcome">&nbsp;欢迎&nbsp; ${name}</span>`)
  }

  layui.element.render('nav','header-nav')
}

